/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* **********************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Theme_Builder
{
    /// <summary>
    /// Encapsulates the ability to edit a specified color and apply a set of customizations to said color
    /// </summary>
    public partial class EditColor : UserControl
    {
        public EditColor()
        {
            InitializeComponent();
        }

        public Color Color
        {
            get
            {
                return this.panelColor.BackColor;
            }
            set
            {
                this.panelColor.BackColor = value;
            }
        }

        public OfficeOpenXMLInterop.CT_SolidColorFillProperties SolidFill
        {
            get
            {
                return this.GetSolidFill();
            }
            set
            {
                this.SetSolidFill(value);
            }
        }

        private OfficeOpenXMLInterop.CT_SolidColorFillProperties GetSolidFill()
        {
            OfficeOpenXMLInterop.CT_SolidColorFillProperties fill = new OfficeOpenXMLInterop.CT_SolidColorFillProperties();

            if (this.comboBoxColorChooser.Text == "Custom Color")
            {
                // TODO
                System.Diagnostics.Debug.Assert(false, "Not Yet Implemented");
            }
            else if (this.comboBoxColorChooser.Text == "System Color")
            {
                // TODO
                System.Diagnostics.Debug.Assert(false, "Not Yet Implemented");
            }
            else
            {
                fill.schemeClr = new OfficeOpenXMLInterop.CT_SchemeColor();
                fill.schemeClr.val = (OfficeOpenXMLInterop.ST_SchemeColorVal)this.comboBoxColorChooser.SelectedIndex;
                ColorMods mods = GetColorMods();
                Converters.ParseModifications(ref fill.schemeClr, mods);
            }

            return fill;
        }

        private ColorMods GetColorMods()
        {
            return editColorMods1.ColorModifications;
        }

        private void SetSolidFill(OfficeOpenXMLInterop.CT_SolidColorFillProperties fill)
        {
            if (fill.schemeClr != null)
            {
                this.comboBoxColorChooser.SelectedIndex = (int)fill.schemeClr.val;
                editColorMods1.ColorModifications = Converters.GetColorModifications(fill.schemeClr);
            }
            else if (fill.sysClr != null)
            {
                // TODO
                System.Diagnostics.Debug.Assert(false, "Not Yet Implemented");
            }
            else if (fill.srgbClr != null)
            {
                // TODO
                System.Diagnostics.Debug.Assert(false, "Not Yet Implemented");
            }
            else
            {
                // TODO
                System.Diagnostics.Debug.Assert(false, "Not Yet Implemented");
            }
        }

        private void panelColor_Click(object sender, EventArgs e)
        {
            this.colorDialog1.Color = this.panelColor.BackColor;

            if (this.colorDialog1.ShowDialog(this) == DialogResult.OK)
            {
                this.panelColor.BackColor = this.colorDialog1.Color;
            }
        }

        private void comboBoxColorChooser_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.panelColor.Visible = comboBoxColorChooser.SelectedIndex == 2;          // Custom Color
            this.comboBoxSystemColor.Visible = comboBoxColorChooser.SelectedIndex == 1; // System Color
            this.editColorMods1.Enabled = comboBoxColorChooser.SelectedIndex != 2;      // Enabled for any non-custom color.

            // Automatically launch the color picker dialog when the user chooses a custom color and one
            //  is currently not chosen
            if (comboBoxColorChooser.SelectedIndex == 2 && this.panelColor.BackColor == Color.White)
            {
                this.panelColor_Click(null, null);
            }
        }
    }
}
